home *** CD-ROM | disk | FTP | other *** search
/ JCSM Shareware Collection 1996 September / JCSM Shareware Collection (JCS Distribution) (September 1996).ISO / prgtools / euphor13.zip / MACHINE.E < prev    next >
Text File  |  1995-05-14  |  3KB  |  103 lines

  1.         ----------------------------------------
  2.         -- Machine Level Programming for 386+ --
  3.         ----------------------------------------
  4.  
  5. -- Warning: Some of these routines require a knowledge of 
  6. -- machine-level programming. You could crash your system.
  7. -- Only superficial checking of argument values is provided.
  8.  
  9. -- These routines, along with peek(), poke() and call(), let you access all 
  10. -- of the features of your computer.  You can read and write to any memory 
  11. -- location, and you can create and execute machine code subroutines.
  12.  
  13. -- Writing characters to screen memory with poke() is much faster than  
  14. -- using puts().
  15. -- address of start of text screen memory 
  16. --       mono: #B0000
  17. --      color: #B8000
  18.  
  19. -- see demo\callmach.ex for an example of calling a machine language routine
  20.  
  21. constant M_ALLOC = 16,
  22.      M_FREE = 17
  23.  
  24. -- biggest address on a 32-bit machine
  25. constant MAX_ADDR = power(2, 32)-1
  26.  
  27. type machine_addr(atom a)
  28. -- a legal machine address 
  29.     return a > 0 and a <= MAX_ADDR and floor(a) = a
  30. end type
  31.  
  32. global function allocate(integer n)
  33. -- allocate n bytes of memory and return the address
  34.     return machine_func(M_ALLOC, n)
  35. end function
  36.  
  37. global procedure free(machine_addr a)
  38. -- free the memory at address a
  39.     machine_proc(M_FREE, a)
  40. end procedure
  41.  
  42. global function int_to_bytes(atom x)
  43. -- returns value of x as a sequence of 4 bytes 
  44. -- that you can poke into memory 
  45. --      {bits 0-7,  (least significant)
  46. --       bits 8-15,
  47. --       bits 16-23,
  48. --       bits 24-31} (most significant)
  49. -- This is the order of bytes in memory on 386+ machines.
  50.     integer a,b,c,d
  51.     
  52.     a = remainder(x, #100)
  53.     x = floor(x / #100)
  54.     b = remainder(x, #100)
  55.     x = floor(x / #100)
  56.     c = remainder(x, #100)
  57.     x = floor(x / #100)
  58.     d = remainder(x, #100)
  59.     return {a,b,c,d}
  60. end function
  61.  
  62. global function bytes_to_int(sequence s)
  63. -- converts 4-byte peek() sequence into an integer value
  64.     return s[1] + 
  65.        s[2] * #100 + 
  66.        s[3] * #10000 + 
  67.        s[4] * #1000000
  68. end function
  69.  
  70. global function int_to_bits(atom x, integer nbits)
  71. -- Returns the low-order nbits bits of x as a sequence of 1's and 0's. 
  72. -- Note that the least significant bits come first. You can use Euphoria's
  73. -- and/or/not operators on sequences of bits. You can also subscript, 
  74. -- slice, concatenate etc. to manipulate bits.
  75.     sequence bits
  76.     
  77.     if x < 0 then
  78.     x = x + power(2, nbits) -- provide 2's complement bit pattern
  79.     end if
  80.     bits = repeat(0, nbits)
  81.     for i = 1 to nbits do
  82.     bits[i] = remainder(x, 2) 
  83.     x = floor(x / 2)
  84.     end for
  85.     return bits
  86. end function
  87.  
  88. global function bits_to_int(sequence bits)
  89. -- get the (positive) value of a sequence of "bits"
  90.     atom value, p
  91.     
  92.     value = 0
  93.     p = 1
  94.     for i = 1 to length(bits) do
  95.     if bits[i] then
  96.         value = value + p
  97.     end if
  98.     p = p + p
  99.     end for
  100.     return value
  101. end function
  102.  
  103.